/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.web30.idm.openldap;

import org.web30.idm.model.IDMException;
import org.web30.idm.model.Web30Person;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPEntry;

/**
 *
 * @author Xia Zhenyu
 */
public class Web30InetOrgPerson implements Web30Person {

    private String displayName = null;
    /**
     * Identity of user, be assigned as LDAP attribute uid
     */
    private String identity = null;
    /**
     * Password of user, be assigned as LDAP attribute userpassword
     */
    private String credential = null;
    /**
     * Mail of user, be assigned as LDAP attribute mail
     */
    private String mail = null;
    /**
     * Organization of user, be assigned as subtree
     */
    private String organization = null;

    /**
     * Construtor
     * @param displayName
     * @param identity
     * @param credential
     * @param mail
     * @param orgnazation
     */
    public Web30InetOrgPerson(String displayName, String identity,
            String credential, String mail, String orgnazation) {
        this.displayName = displayName;
        this.identity = identity;
        this.credential = credential;
        this.mail = mail;
        this.organization = orgnazation;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getCredential() {
        return credential;
    }

    public void setCredential(String credential) {
        this.credential = credential;
    }

    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String orgnazation) {
        this.organization = orgnazation;
    }

    /**
     * Get LDAP entry, lib supplied by Novell LDAP suite
     * @return
     * @throws IDMException
     */
    public LDAPEntry getLDAPEntry() throws IDMException {
        this.entityValidation();
        LDAPAttributeSet attributeSet = new LDAPAttributeSet();

        /* To Add an entry to the directory,

         *  - Create the attributes of the entry and add them to an attribute set

         *  - Specify the DN of the entry to be created

         *  - Create an LDAPEntry object with the DN and the attribute set

         *  - Call the LDAPConnection add method to add it to the directory

         */

        attributeSet.add(new LDAPAttribute(
                Web30Constants.LDAP_SCHAME_OBJ,
                Web30Constants.LDAP_SCHAME_OBJ_NAME));

        attributeSet.add(new LDAPAttribute(
                Web30Constants.LDAP_SCHAME_COMMON_NAME,
                this.getIdentity()));

        attributeSet.add(new LDAPAttribute(
                Web30Constants.LDAP_SCHAME_SURNAME,
                this.getIdentity()));

        attributeSet.add(new LDAPAttribute(
                Web30Constants.LDAP_SCHEMA_UID,
                this.getIdentity()));

        attributeSet.add(
                new LDAPAttribute(
                Web30Constants.LDAP_SCHAME_MAIL,
                this.getMail()));

        attributeSet.add(
                new LDAPAttribute(
                Web30Constants.LDAP_SCHAME_PASSWORD,
                this.getCredential()));

        LDAPEntry newEntry = new LDAPEntry(this.getDN(), attributeSet);

        return newEntry;
    }

    /**
     * Validate Entity, here check must attribute of LDAPEntry
     * @throws IDMException
     */
    public void entityValidation() throws IDMException {
        if (this.getCredential() == null ||
                this.getIdentity() == null ||
                this.getMail() == null) {
            throw new IDMException(IDMException.IDM_INSUFFICIENT_ATTR);
        }
    }

    /**
     * Get DN of this LDAP Entry
     * @return
     */
    public String getDN() {
        String dn = "uid=" + this.getIdentity() + ",dc=" + this.getOrganization() +
                "," + Web30Constants.DN_SUFFIX;
        return dn;
    }
}
